#include "common.h"
#include "minterface.h"
#include "glwindow.h"
#include "stb_image.h"

bool MetamorphInterface::render(void)
{
	glViewport(TRACER_SIZE, 0, INTERFACE_SIZE, TRACER_SIZE);
	glLoadIdentity();
	glEnable(GL_TEXTURE_2D);
	if (bPressed)
		glBindTexture(GL_TEXTURE_2D, iSampleTexture2);
	else
		glBindTexture(GL_TEXTURE_2D, iSampleTexture1);
	glBegin(GL_QUADS);
	glColor3f(1.0f, 0.0f, 0.0f);
	glTexCoord2f(0,1);
	glVertex3f(-1.0f,  -1.0f, 0.0f);
	glTexCoord2f(0,0);
	glVertex3f(-1.0f,  1.0f, 0.0f);
	glTexCoord2f(1,0);
	glVertex3f(1.0f,  1.0f, 0.0f);
	glTexCoord2f(1,1);
	glVertex3f(1.0f,  -1.0f, 0.0f);
	glEnd();
	glFlush();
	glDisable(GL_TEXTURE_2D);
	glEnable(GL_LIGHTING);
	return true;
}

void MetamorphInterface::init(void)
{
	glGenTextures(1, &iSampleTexture1);
	glBindTexture(GL_TEXTURE_2D, iSampleTexture1);

	{
	int nWidth, nHeight, nBpp;
	unsigned char * pRgb;
	FILE * pf;
	pf = fopen("interface/tut1.jpg", "rb");
	pRgb = stbi_load_from_file( pf, &nWidth, &nHeight, &nBpp, 3 );
	assert (pRgb != NULL);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, nWidth, nHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, pRgb);
	stbi_image_free( pRgb );
	fclose(pf);
	}

	glGenTextures(1, &iSampleTexture2);
	glBindTexture(GL_TEXTURE_2D, iSampleTexture2);
	{
	int nWidth, nHeight, nBpp;
	unsigned char * pRgb;
	FILE * pf;
	pf = fopen("interface/tut2.jpg", "rb");
	pRgb = stbi_load_from_file( pf, &nWidth, &nHeight, &nBpp, 3 );
	assert (pRgb != NULL);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, nWidth, nHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, pRgb);
	stbi_image_free( pRgb );
	fclose(pf);
	}
}

void MetamorphInterface::onMousePressed(int x, int y)
{
	bPressed = true;
}

void MetamorphInterface::onMouseReleased(int x, int y)
{
	bPressed = false;
}

void MetamorphInterface::onButtonPressed(int code)
{
	bPressed = true;
}

void MetamorphInterface::onButtonReleased(int code)
{
	bPressed = false;
}
